﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skyhook.Core;
using Skyhook.Resources;

namespace Skyhook.Sprites
{
    internal class SpriteFactory
    {
        internal SpriteFactory()
        {
        }

        private SpriteObject CreateSpriteObject(ImageCollection image, int[] groupsnum, string layername, IRenderer renderer = null)
        {
            SpriteLayersLayout layerslayout = SpriteLayersLayoutManager.CreateLayout(layername);

            SpriteObject sprite = new SpriteObject(image, groupsnum, layerslayout,(ISpriteRenderer)renderer);

            return sprite;
        }

        public SpriteObject CreateSprite(string spritename, ImageCollection image, int[] groupsnum, string layername, IRenderer renderer = null)
        {
            SpriteObject result = CacheSprite(spritename, image, groupsnum, layername, renderer);

            result = result.Clone();
            return result;
        }

        private SpriteObject CacheSprite(string spritename, ImageCollection image, int[] groupsnum, string layername, IRenderer renderer = null)
        {
            SpriteObject result = null;
            result = SpriteCache.GetSpriteObject(spritename);
            if (result == null)
            {
                result = CreateSpriteObject(image, groupsnum, layername, renderer);
                SpriteCache.AddSpriteObject(spritename, result);
            }
            return result;
        }
    }
}
